﻿using HttpClient.HTTP;
using HttpClient.Models;
using HttpClient.Restful;
using Nancy.Json;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HttpClient
{
    public partial class FrmUserEdit : Form
    {
        // 自定义http请求对象
        private WebClient client;
        private bool b_Create = false;

        public FrmUserEdit()
        {
            InitializeComponent();
            client = new WebClient();
        }

        private void FrmUserEdit_Load(object sender, EventArgs e)
        {

        }

        public void Init(User user, bool b_Create)
        {
            this.b_Create = b_Create;

            txtID.Text = "";
            txtName.Text = "";
            txtAge.Text = "";

            if (!b_Create)
            {
                txtID.Enabled = false;
                txtID.Text = user.Id.ToString();
                txtName.Text = user.Name;
                txtAge.Text = user.Age.ToString();
            }
            else
            {
                txtID.Enabled = true;
            }
        }


        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.b_Create)
            {
                InsertUser();
            }
            else
            {
                UpdateUser();
            }
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        private void InsertUser()
        {
            try
            {
                User user = new User
                {
                    Id = int.Parse(txtID.Text),
                    Name = txtName.Text,
                    Age = int.Parse(txtAge.Text)
                };
                if (user.Id <= 0 || string.IsNullOrWhiteSpace(user.Name) || user.Age <= 0)
                {
                    MessageBox.Show("用户信息校验失败！", "错误");
                }


                string url = "http://127.0.0.1:8087/test/user/insert";
                /*string body = new JavaScriptSerializer().Serialize(user);*/
                string body = JsonConvert.SerializeObject(user);
                string result = client.HttpPost(url, body);
                JsonResult jsonResult = JsonConvert.DeserializeObject<JsonResult>(result);
                if (jsonResult == null)
                {
                    MessageBox.Show("请求异常！", "错误");
                    return;
                }
                if (jsonResult.Code == 200)
                {
                    MessageBox.Show("Success！", "提示");
                    this.Close();
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="user"></param>
        private void UpdateUser()
        {
            try
            {
                User user = new User
                {
                    Id = int.Parse(txtID.Text),
                    Name = txtName.Text,
                    Age = int.Parse(txtAge.Text)
                };
                if (user.Id <= 0 || string.IsNullOrWhiteSpace(user.Name) || user.Age <= 0)
                {
                    MessageBox.Show("用户信息校验失败！", "错误");
                }

                string url = "http://127.0.0.1:8087/test/user/update";
                /*string body = new JavaScriptSerializer().Serialize(user);*/
                string body = JsonConvert.SerializeObject(user);
                string result = client.HttpPost(url, body);
                JsonResult jsonResult = JsonConvert.DeserializeObject<JsonResult>(result);
                if (jsonResult == null)
                {
                    MessageBox.Show("请求异常！", "错误");
                    return;
                }
                if (jsonResult.Code == 200)
                {
                    MessageBox.Show("Success！", "提示");
                    this.Close();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
        }




        private void btnCanel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
